package com.example.demo20250517.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo20250517.domain.Question;
import com.example.demo20250517.domain.Result;
import com.example.demo20250517.domain.ResultItem;
import com.example.demo20250517.mapper.QuestionItemMapper;
import com.example.demo20250517.mapper.QuestionMapper;
import com.example.demo20250517.mapper.ResultItemMapper;
import com.example.demo20250517.mapper.ResultMapper;
import com.example.demo20250517.service.QuestionService;
import com.example.demo20250517.vo.QuestionAddVo;
import com.example.demo20250517.vo.QuestionConfigDetailVo;
import com.example.demo20250517.vo.QuestionResultAddDetailVo;
import com.example.demo20250517.vo.QuestionResultAddVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author banban
 * @description 针对表【question】的数据库操作Service实现
 * @createDate 2025-05-17 10:33:17
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
        implements QuestionService {

    @Resource
    private QuestionItemMapper questionItemMapper;
    @Resource
    private ResultItemMapper resultItemMapper;

    @Override
    public Boolean add(QuestionAddVo questionAddVo) {

        //插入题库表
        Question question = new Question();
        BeanUtils.copyProperties(questionAddVo, question);
        question.setConfig(JSON.toJSONString(questionAddVo.getConfigList()));
        baseMapper.updateById(question);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        //删除题库
        baseMapper.deleteQuestion(id);
        //删除题目

        return true;
    }

    @Resource
    private ResultMapper resultMapper;

    @Override
    public Result addResult(QuestionResultAddVo questionResultAddVo) {
        List<QuestionResultAddDetailVo> itemList = questionResultAddVo.getItemList();
        //题库id
        Long questionId = questionItemMapper.selectById(itemList.get(0).getItemId()).getQuestionId();
        //todo 调用分布式id获取主键id
        Long resultId = 0l;

        Integer score = 0;
        for (QuestionResultAddDetailVo questionResultAddDetailVo : itemList) {
            ResultItem tmp = new ResultItem();
            tmp.setQuestionId(questionId);
            tmp.setResultId(resultId);
            tmp.setOption(questionResultAddDetailVo.getOption());
            //获取userId
            tmp.setUserId(1L);
            tmp.setItemId(questionResultAddDetailVo.getItemId());
            resultItemMapper.insert(tmp);

            score += questionResultAddDetailVo.getScore();
        }

        Question question = baseMapper.selectById(questionId);
        String config = question.getConfig();
        List<QuestionConfigDetailVo> configList = JSON.parseArray(config, QuestionConfigDetailVo.class);

        Result result = new Result();
        JSONObject jsonObject = new JSONObject();

        for (QuestionConfigDetailVo tmpLista : configList) {
            if (tmpLista.getMin() < score && tmpLista.getMax() >= score) {
                String suggest = tmpLista.getSuggest();
                String summarize = tmpLista.getSummarize();
                String indicator = tmpLista.getIndicator();
                result.setSuggest(suggest);
                result.setSummarize(summarize);
                jsonObject.put("result", indicator);
                jsonObject.put("score", score);
                jsonObject.put("totalScore", );
                result.setDetail(jsonObject.toJSONString());
                resultMapper.insert(result);
            }

        }
        return result;
    }
}




